############################################################
## Fraga, Juenke, and Shah 2019: One Run Leads to Another?
## Replication Code: January 2, 2018
## Figure A7: Same as Figure 1, but Democratic Candidate Emergence with Democratic Co-Ethnic Incumbents
## Figure A8: Same as Figure 1, but Republican Candidate Emergence with Republican Co-Ethnic Incumbents
#############################################################

# Required Libraries
require(ggplot2)

# Load Data #
sl_data <- read.csv("FragaJuenkeShah_JOP_Data.csv", stringsAsFactors=FALSE)

# Remove all districts where 1+ candidates have no race data
sl_data <- subset(sl_data, unknown_cand == 0)

## FigA7a, Upper Left Panel of Figure A7: Whites
sl_dataY <- subset(sl_data, white_incprop_D >= 0.25)
sl_dataN <- subset(sl_data, white_incprop_D < 0.25)

figA7a <- ggplot(data=sl_data, aes(x=white_pct, y=white_cand_D))
figA7a <- figA7a + geom_segment(aes(y=0.5, yend=0.5, x=-Inf, xend=Inf), size=0.75, linetype=1, color="gray80")
figA7a <- figA7a + stat_smooth(data=sl_dataN, method = "loess", size=2, color="black")
figA7a <- figA7a + stat_smooth(data=sl_dataY, method = "loess", size=2, color="gray40", linetype="longdash")
figA7a <- figA7a + ggtitle("Whites")
figA7a <- figA7a + scale_x_continuous("Percent White in State Legislative District")
figA7a <- figA7a + scale_y_continuous("Probability of White Candidate")
figA7a <- figA7a + coord_cartesian(ylim=c(-0.01, 1.01))
figA7a <- figA7a + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
figA7a

## FigA7b, Upper Right Panel of Figure A7: African-Americans
sl_dataY <- subset(sl_data, black_incprop_D >= 0.25)
sl_dataN <- subset(sl_data, black_incprop_D < 0.25)

figA7b <- ggplot(data=sl_data, aes(x=black_pct, y=black_cand_D))
figA7b <- figA7b + geom_segment(aes(y=0.5, yend=0.5, x=-Inf, xend=Inf), size=0.75, linetype=1, color="gray80")
figA7b <- figA7b + stat_smooth(data=sl_dataN, method = "loess", size=2, color="black")
figA7b <- figA7b + stat_smooth(data=sl_dataY, method = "loess", size=2, color="gray40", linetype="longdash")
figA7b <- figA7b + ggtitle("African-Americans")
figA7b <- figA7b + scale_x_continuous("Percent Black in State Legislative District")
figA7b <- figA7b + scale_y_continuous("Probability of Black Candidate")
figA7b <- figA7b + coord_cartesian(ylim=c(-0.01, 1.01))
figA7b <- figA7b + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
figA7b

## FigA7c, Lower Left Panel of Figure A7: Latinos
sl_dataY <- subset(sl_data, latino_incprop_D >= 0.25)
sl_dataN <- subset(sl_data, latino_incprop_D < 0.25)

figA7c <- ggplot(data=sl_data, aes(x=latino_pct, y=latino_cand_D))
figA7c <- figA7c + geom_segment(aes(y=0.5, yend=0.5, x=-Inf, xend=Inf), size=0.75, linetype=1, color="gray80")
figA7c <- figA7c + stat_smooth(data=sl_dataN, method = "loess", size=2, color="black")
figA7c <- figA7c + stat_smooth(data=sl_dataY, method = "loess", size=2, color="gray40", linetype="longdash")
figA7c <- figA7c + ggtitle("Latinos")
figA7c <- figA7c + scale_x_continuous("Percent Latino in State Legislative District")
figA7c <- figA7c + scale_y_continuous("Probability of Latino Candidate")
figA7c <- figA7c + coord_cartesian(ylim=c(-0.01, 1.01))
figA7c <- figA7c + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
figA7c

## FigA7d, Lower Right Panel of Figure A7: Asians
sl_dataY <- subset(sl_data, asian_incprop_D >= 0.25)
sl_dataN <- subset(sl_data, asian_incprop_D < 0.25)

figA7d <- ggplot(data=sl_data, aes(x=asian_pct, y=asian_cand_D))
figA7d <- figA7d + geom_segment(aes(y=0.5, yend=0.5, x=-Inf, xend=Inf), size=0.75, linetype=1, color="gray80")
figA7d <- figA7d + stat_smooth(data=sl_dataN, method = "loess", size=2, color="black")
figA7d <- figA7d + stat_smooth(data=sl_dataY, method = "loess", size=2, color="gray40", linetype="longdash")
figA7d <- figA7d + ggtitle("Asians")
figA7d <- figA7d + scale_x_continuous("Percent Asian in State Legislative District")
figA7d <- figA7d + scale_y_continuous("Probability of Asian Candidate")
figA7d <- figA7d + coord_cartesian(ylim=c(-0.01, 1.01))
figA7d <- figA7d + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
figA7d

## FigA8a, Left Panel of Figure A8: Whites
sl_dataY <- subset(sl_data, white_incprop_R >= 0.25)
sl_dataN <- subset(sl_data, white_incprop_R < 0.25)

figA8a <- ggplot(data=sl_data, aes(x=white_pct, y=white_cand_R))
figA8a <- figA8a + geom_segment(aes(y=0.5, yend=0.5, x=-Inf, xend=Inf), size=0.75, linetype=1, color="gray80")
figA8a <- figA8a + stat_smooth(data=sl_dataN, method = "loess", size=2, color="black")
figA8a <- figA8a + stat_smooth(data=sl_dataY, method = "loess", size=2, color="gray40", linetype="longdash")
figA8a <- figA8a + ggtitle("Whites")
figA8a <- figA8a + scale_x_continuous("Percent White in State Legislative District")
figA8a <- figA8a + scale_y_continuous("Probability of White Candidate")
figA8a <- figA8a + coord_cartesian(ylim=c(-0.01, 1.01))
figA8a <- figA8a + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
figA8a

## FigA8b, Right Panel of Figure A8: Latinos
sl_dataY <- subset(sl_data, latino_incprop_R >= 0.25)
sl_dataN <- subset(sl_data, latino_incprop_R < 0.25)

figA8b <- ggplot(data=sl_data, aes(x=latino_pct, y=latino_cand_R))
figA8b <- figA8b + geom_segment(aes(y=0.5, yend=0.5, x=-Inf, xend=Inf), size=0.75, linetype=1, color="gray80")
figA8b <- figA8b + stat_smooth(data=sl_dataN, method = "loess", size=2, color="black")
figA8b <- figA8b + stat_smooth(data=sl_dataY, method = "loess", size=2, color="gray40", linetype="longdash")
figA8b <- figA8b + ggtitle("Latinos")
figA8b <- figA8b + scale_x_continuous("Percent Latino in State Legislative District")
figA8b <- figA8b + scale_y_continuous("Probability of Latino Candidate")
figA8b <- figA8b + coord_cartesian(ylim=c(-0.01, 1.01))
figA8b <- figA8b + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
figA8b